<template>
  <div class="machine">
    <template v-for="(item, index) in sm" :key="item.machine_id">
      <template v-if="item.is_show == null || item.is_show != 0">
        <template v-if="props.selectVal == '全部' ? true : getIsShow(item)">
          <div class="sm_body">
            <!-- <template> -->
            <div class="sm_area" v-if="item.bindType == 'area' ? true : false">
              <div class="k_area">
                <template v-if="item.area_empty.status == 1">
                  <div class="yb" :style="computeStyle(item.area_empty)" @click.stop="handlerAreaPointClick(item.area_empty, 0)">
                    <template v-for="subitem in item.area_empty.point" :key="subitem.point_id">
                      <div
                        :class="subitem.pstatus_point == '未识别' ? 'yb-point-undefined' : 'yb-point'"
                        :style="bucketStyle(subitem)"
                        @click.stop="handlerAreaPointClick(item.area_empty, subitem.point_number)"
                      >
                        {{ subitem.point_number }}
                      </div>
                    </template>
                  </div>
                </template>
                <template v-if="item.area_empty.status == 0">
                  <div class="yb" :style="computeStyle(item.area_empty)"></div>
                </template>
                <div class="yb_name" :style="computeNameStyle(item.area_empty)" @click="handlerAreaClick(item.area_empty)">
                  {{ item.area_empty.area_name + ' - ' + item.product_name + '线' }}
                </div>
              </div>
              <div class="m_area">
                <template v-if="item.area_full.status == 1">
                  <div class="yb" :style="computeStyle(item.area_full)" @click.stop="handlerAreaPointClick(item.area_full, 0)">
                    <template v-for="subitem in item.area_full.point" :key="subitem.point_id">
                      <div
                        :class="subitem.pstatus_point == '未识别' ? 'yb-point-undefined' : 'yb-point'"
                        :style="bucketStyle(subitem)"
                        @click.stop="handlerAreaPointClick(item.area_full, subitem.point_number)"
                      >
                        {{ subitem.pstatus_iswork == '忙碌' ? subitem.point_work_site : subitem.point_number }}
                      </div>
                    </template>
                  </div>
                </template>
                <template v-if="item.area_full.status == 0">
                  <div class="yb" :style="computeStyle(item.area_full)"></div>
                </template>
                <div class="yb_name" :style="computeNameStyle(item.area_full)" @click="handlerAreaClick(item.area_full)">
                  {{ item.area_full.area_name + ' - ' + item.product_name + '线' }}
                </div>
              </div>
            </div>
            <template v-else>
              <div class="sm">
                <template v-if="item.process_name == '梳并工序段'">
                  <div class="sm_item" v-if="setting.sm.image == '默认' || setting.sm.image == undefined">
                    <div class="sm_k" :style="KStyle(item)" @click="handlerPointClick('空桶', item.point_empty)"></div>
                    <div class="sm_m" :style="MStyle(item)" @click="handlerPointClick('满桶', item.point_full)"></div>
                  </div>
                  <template v-if="setting.sm.image == '德永盛'">
                    <div class="sm_other_item_zuo" v-if="index == 0 || index % 2 == 0">
                      <div class="sm_k" :style="MStyle(item)" @click="handlerPointClick('复用', item.point_full)"></div>
                      <!-- <div class="sm_m" :style="MStyle(item)" @click="handlerPointClick('满桶', item.point_full)"></div> -->
                    </div>
                    <div class="sm_other_item_you" v-if="index % 2 !== 0">
                      <div class="sm_k" :style="MStyle(item)" @click="handlerPointClick('复用', item.point_full)"></div>
                      <!-- <div class="sm_m" :style="MStyle(item)" @click="handlerPointClick('满桶', item.point_full)"></div> -->
                    </div>
                  </template>
                </template>
                <template v-else-if="item.process_name == '并粗工序段'">
                  <template v-if="item.machine_side == '左侧'">
                    <div class="mb_item_left">
                      <div class="mb_k_left" :style="KStyle(item)" @click="handlerPointClick('空桶', item.point_empty)"></div>
                      <div class="mb_m_left" :style="MStyle(item)" @click="handlerPointClick('满桶', item.point_full)"></div>
                    </div>
                  </template>
                  <template v-if="item.machine_side == '右侧'">
                    <div class="mb_item_right">
                      <div class="mb_k_right" :style="KStyle(item)" @click="handlerPointClick('空桶', item.point_empty)"></div>
                      <div class="mb_m_right" :style="MStyle(item)" @click="handlerPointClick('满桶', item.point_full)"></div>
                    </div>
                  </template>
                </template>
                <div class="sm_pro">产线:{{ item.product_name }}</div>
              </div>
              <div class="sm_name" :style="computeSmStyle(item)" @click="handlerMachineClick(item)">{{ item.machine_name }}</div>
            </template>
            <!-- </template> -->
          </div>
        </template>
      </template>
    </template>
    <MachineModal ref="machineModalRef"></MachineModal>
    <PointModal ref="pointModalRef"></PointModal>
    <AreaModal ref="areaModalRef"></AreaModal>
    <AreaPointModal ref="areaPointModalRef"></AreaPointModal>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import MachineModal from './machine-modal.vue'
import AreaModal from './area-modal.vue'
import PointModal from './point-modal.vue'
import { Message } from '@arco-design/web-vue'
import AreaPointModal from './areaPoint-modal.vue'
import { storeToRefs } from 'pinia'
import useMainStore from '../../../store/main/main'
import useSetStore from '../../../store/setting/setting.js'
const setStore = useSetStore()
const { setting } = storeToRefs(setStore)
const mainStore = useMainStore()
const { sm, product, group } = storeToRefs(mainStore)
const props = defineProps({
  selectVal: {
    type: String
  }
})
function computeSmStyle(item) {
  let style = {
    backgroundColor: ''
  }
  if (item.status == 0) {
    style.backgroundColor = 'red'
  } else if (item.status == 1) {
    if (item.sm_auto == 1) {
      style.backgroundColor = 'green'
    } else {
      style.backgroundColor = '#1e9ad8'
    }
  } else if (item.status == 2) {
    style.backgroundColor = '#ccc'
  }
  return style
}
function KStyle(item) {
  let style = {
    backgroundColor: '',
    border: ''
  }
  if (item.point_empty != '') {
    if (item.point_empty.status == 1) {
      if (item.point_empty.pstatus_iswork == '空闲') {
        if (item.point_empty.pstatus_point == '满桶') {
          style.backgroundColor = '#1c62bb'
        } else if (item.point_empty.pstatus_point == '空桶') {
          style.backgroundColor = '#fff'
          style.border = '3px solid #157ae3'
        } else if (item.point_empty.pstatus_point == '禁止作业') {
          style.backgroundColor = '#fff'
          style.border = '3px solid red'
        }
      } else if (item.point_empty.pstatus_iswork == '忙碌') {
        style.backgroundColor = 'rgb(255, 179, 0)'
      }
    } else if (item.point_empty.status == 0) {
      style.background = 'url(/static/images/11.png) no-repeat center center / cover'
    }
  }
  return style
}
function MStyle(item) {
  let style = {
    backgroundColor: '',
    border: ''
  }
  if (item.point_full != '') {
    if (item.point_full.status == 1) {
      if (item.point_full.pstatus_iswork == '空闲') {
        if (item.point_full.pstatus_point == '满桶') {
          style.backgroundColor = '#1c62bb'
        } else if (item.point_full.pstatus_point == '空桶') {
          style.backgroundColor = '#fff'
          style.border = '3px solid #157ae3'
        } else if (item.point_full.pstatus_point == '禁止作业') {
          style.backgroundColor = '#fff'
          style.border = '3px solid red'
        }
      } else if (item.point_full.pstatus_iswork == '忙碌') {
        style.backgroundColor = 'rgb(255, 179, 0)'
      }
    } else if (item.point_full.status == 0) {
      style.background = 'url(/static/images/11.png) no-repeat center center / cover'
    }
  }
  return style
}
const machineModalRef = ref()
function handlerMachineClick(item) {
  machineModalRef.value?.setMachineModalVisible(item)
}
const pointModalRef = ref()
function handlerPointClick(type, item) {
  if (item.pstatus_iswork != '忙碌') {
    pointModalRef.value?.setPointModalVisible(type, item)
  } else {
    Message.warning('该点位正在工作中，禁止盘库!')
  }
}
function computeStyle(item) {
  let style = {
    backgroundColor: '',
    width: setting.value.ybArea.number * 80 + 'px'
  }
  if (item.status == 0) {
    style.backgroundColor = 'red'
  } else if (item.status == 1) {
    if (item.area_worktype == '空桶区') {
      style.backgroundColor = '#cff'
    } else if (item.area_worktype == '满桶区') {
      style.backgroundColor = '#ffff9a'
    } else if (item.area_worktype == '复用区') {
      style.backgroundColor = '#ffd8be'
    }
  }
  return style
}
function computeNameStyle(item) {
  let style = {
    backgroundColor: '',
    width: setting.value.ybArea.number * 80 + 'px'
  }
  if (item.status == 0) {
    style.backgroundColor = 'red'
  }
  if (item.status == 1) {
    if (item.sm_auto == 1) {
      style.backgroundColor = 'green'
    } else {
      style.backgroundColor = '#1e9ad8'
    }
  }
  return style
}
function bucketStyle(subitem) {
  let style = {
    backgroundColor: '',
    border: '',
    animation: ''
  }
  if (subitem.status == 1) {
    if (subitem.pstatus_iswork == '空闲') {
      if (subitem.pstatus_point == '满桶') {
        style.backgroundColor = '#1c62bb'
        style.border = '4px solid #1c62bb'
      } else if (subitem.pstatus_point == '空桶') {
        style.backgroundColor = '#fff'
        style.border = '4px solid #1c62bb'
      } else if (subitem.pstatus_point == '无桶') {
        // style.backgroundColor = '#fff'
        style.border = '4px solid #ccc'
      } else if (subitem.pstatus_point == '未识别') {
        style.border = ''
      } else if (subitem.pstatus_point == '未读') {
        style.backgroundColor = '#ccc'
        style.border = '4px solid #000'
      } else if (subitem.pstatus_point == '禁止作业') {
        style.backgroundColor = '#fff'
        style.border = '4px solid red'
      }
    } else {
      style.backgroundColor = 'rgb(255, 179, 0)'
      style.border = '4px solid rgb(255, 179, 0)'
    }
  } else if (subitem.status == 0) {
    style.backgroundColor = 'red'
    style.border = '4px solid red'
  }
  return style
}
const areaModalRef = ref()

function handlerAreaClick(item) {
  areaModalRef.value?.setAreaModalVisible(item)
}

const getIsShow = (machine) => {
  // 遍历 group.value
  if (setting.value.sm.machineSelect == '组') {
    for (let item of group.value) {
      // 检查 group_name 是否匹配
      if (item.group_name === props.selectVal) {
        // 遍历 group_member
        for (let subitem of item.group_member) {
          // 检查 machine_name 是否匹配
          if (machine.machine_name === subitem) {
            return true // 如果找到匹配项，立即返回 true
          }
        }
      }
    }
    return false // 如果没有找到匹配项，返回 false
  } else {
    return props.selectVal == machine.product_name
  }
}
const areaPointModalRef = ref()
function handlerAreaPointClick(item, index) {
  areaPointModalRef.value?.setAreaPointModalVisible(item, index)
}
</script>

<style lang="less" scoped>
.machine {
  padding: 20px 10px;
  height: calc(1080px - 120px);
  overflow: auto;
  scroll-behavior: smooth;
  &::-webkit-scrollbar,
  & *::-webkit-scrollbar {
    width: 0 !important; /* Chrome, Safari, and Opera */
    background: transparent !important;
  }

  &::-webkit-scrollbar-track {
    background: transparent !important;
  }

  &::-webkit-scrollbar-thumb {
    background: transparent !important;
    border: none !important;
  }
  .sm_body {
    float: left;
    position: relative;
    margin: 10px;
    zoom: 1.3;
    .sm {
      display: flex;

      .sm_other_item_zuo {
        height: 216px;
        width: 104px;
        background: url(../../../assets/img/u-zuo.png) no-repeat;
        background-size: 100% 100%;
        .sm_k {
          position: absolute;
          box-sizing: border-box;
          left: 13px;
          top: 172px;
          width: 38px;
          height: 38px;
          border-radius: 50%;
          // border: 3px solid #157ae3;
          // background-color: #fff;
        }
      }
      .sm_other_item_you {
        height: 216px;
        width: 104px;
        background: url(../../../assets/img/u-you.png) no-repeat;
        background-size: 100% 100%;
        .sm_k {
          position: absolute;
          box-sizing: border-box;
          left: 57px;
          top: 172px;
          width: 38px;
          height: 38px;
          border-radius: 50%;
          // border: 3px solid #157ae3;
          // background-color: #fff;
        }
      }
      .sm_item {
        height: 156px;
        width: 104px;
        background: url(../../../assets/img/smj.png) no-repeat;

        .sm_k {
          position: absolute;
          box-sizing: border-box;
          left: 3px;
          top: 118px;
          width: 28px;
          height: 28px;
          border-radius: 50%;
          // border: 3px solid #157ae3;
          // background-color: #fff;
        }
        .sm_m {
          position: absolute;
          box-sizing: border-box;
          left: 73px;
          top: 118px;
          width: 28px;
          height: 28px;
          border-radius: 50%;
          // background-color: #157ae3;
        }
      }
      .mb_item_left {
        height: 216px;
        width: 124px;
        background: url(../../../assets/img/left.png) no-repeat;
        .mb_k_left {
          position: absolute;
          box-sizing: border-box;
          left: 3px;
          top: 188px;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          // border: 3px solid #157ae3;
          // background-color: #fff;
        }
        .mb_m_left {
          position: absolute;
          box-sizing: border-box;
          left: 53px;
          top: 188px;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          // background-color: #157ae3;
        }
      }
      .mb_item_right {
        height: 216px;
        width: 124px;
        background: url(../../../assets/img/right.png) no-repeat;
        .mb_k_right {
          position: absolute;
          box-sizing: border-box;
          left: 60px;
          top: 189px;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          // border: 3px solid #157ae3;
          // background-color: #fff;
        }
        .mb_m_right {
          position: absolute;
          box-sizing: border-box;
          left: 30px;
          top: 189px;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          // background-color: #157ae3;
        }
      }
      .sm_pro {
        font-size: 18px;
        font-weight: 500;
        width: 70px;
      }
    }
    .sm_name {
      margin-top: 10px;
      width: 160px;
      height: 50px;
      background-color: #4bc44b;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 22px;
    }
  }
}
.sm_area {
  // display: flex;
  .k_area {
    float: left;
    margin-right: 20px;
    position: relative;
    .yb {
      float: left;
      min-height: 167px;
      width: 80px;
      height: auto;
      .yb-point {
        float: left;
        box-sizing: border-box;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin: 8px 10px 8px 10px;
        text-align: center;
        line-height: 52px;
        font-size: 18px;
      }
      .yb-point-undefined {
        float: left;
        box-sizing: border-box;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin: 8px 10px 8px 10px;
        text-align: center;
        line-height: 60px;
        font-size: 18px;
        animation: blink 2s infinite;
      }
    }
    .yb_name {
      // position: absolute;
      // bottom: -60px;
      // margin-top: 10px;
      min-height: 50px;
      background-color: #4bc44b;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 22px;
    }
  }
  .m_area {
    float: left;
    margin: 0 10px;
    position: relative;
    .yb {
      float: left;
      min-height: 167px;
      width: 80px;
      height: auto;
      .yb-point {
        float: left;
        box-sizing: border-box;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin: 8px 10px 8px 10px;
        text-align: center;
        line-height: 52px;
        font-size: 18px;
      }
      .yb-point-undefined {
        float: left;
        box-sizing: border-box;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin: 8px 10px 8px 10px;
        text-align: center;
        line-height: 60px;
        font-size: 18px;
        animation: blink 2s infinite;
      }
    }
    .yb_name {
      // position: absolute;
      // bottom: -60px;
      // margin-top: 10px;
      min-height: 50px;
      background-color: #4bc44b;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 24px;
    }
  }
}
@keyframes blink {
  0% {
    background-color: #1c62bb;
  }
  50% {
    background-color: #4bc44b;
  }
  100% {
    background-color: #1c62bb;
  }
}
</style>
